﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="" file="MenuEntry.cs">
//   
// </copyright>
// <summary>
//   The menu entry.
// </summary>
// 
// --------------------------------------------------------------------------------------------------------------------

/* 
 * Author: Juan Mompean Esteban
 * Created: 26.11.2011
 */
namespace ITU_vs_Zombies.Menu.Screens
{
    using System;
    using System.Diagnostics.Contracts;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// The menu entry.
    /// </summary>
    public class MenuEntry
    {
        #region Constants and Fields

        /// <summary>
        /// The position.
        /// </summary>
        private Vector2 position;

        /// <summary>
        /// The text.
        /// </summary>
        private string text;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MenuEntry"/> class.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        public MenuEntry(string text)
        {
            this.text = text;
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The selected.
        /// </summary>
        public event EventHandler Selected;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets Position.
        /// </summary>
        public Vector2 Position
        {
            get
            {
                return this.position;
            }

            set
            {
                this.position = value;
            }
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        public string Text
        {
            get
            {
                return this.text;
            }

            set
            {
                this.text = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The draw.
        /// </summary>
        /// <param name="screen">
        /// The screen.
        /// </param>
        /// <param name="isSelected">
        /// The is selected.
        /// </param>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            Contract.Requires(gameTime != null);
            Contract.Requires(screen != null);

            Color color = isSelected ? Color.Yellow : Color.Cyan;

            double time = gameTime.TotalGameTime.TotalSeconds;

            float scale = 1;

            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;

            var origin = new Vector2(0, font.LineSpacing / 2);

            spriteBatch.DrawString(font, this.text, this.position, color, 0, origin, scale, SpriteEffects.None, 0);
        }

        /// <summary>
        /// The get height.
        /// </summary>
        /// <param name="screen">
        /// The screen.
        /// </param>
        /// <returns>
        /// The get height.
        /// </returns>
        public virtual int GetHeight(MenuScreen screen)
        {
            Contract.Requires(screen != null);
            return screen.ScreenManager.Font.LineSpacing;
        }

        /// <summary>
        /// The get width.
        /// </summary>
        /// <param name="screen">
        /// The screen.
        /// </param>
        /// <returns>
        /// The get width.
        /// </returns>
        public virtual int GetWidth(MenuScreen screen)
        {
            Contract.Requires(screen != null);
            return (int)screen.ScreenManager.Font.MeasureString(this.Text).X;
        }

        /// <summary>
        /// The update.
        /// </summary>
        /// <param name="screen">
        /// The screen.
        /// </param>
        /// <param name="isSelected">
        /// The is selected.
        /// </param>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on select entry.
        /// </summary>
        protected internal virtual void OnSelectEntry()
        {
            if (this.Selected != null)
            {
                this.Selected(this, null);
            }
        }

        #endregion
    }
}